package br.gov.cemaden.cliente.precipitacao;

import br.gov.cemaden.cliente.precipitacao.exception.ObjectNotFoundException;
import br.gov.cemaden.library.dao.MunicipioRepository;
import br.gov.cemaden.library.model.Municipio;
import br.gov.cemaden.library.model.previsao.AquisicaoPrecipitacao;
import br.gov.cemaden.library.model.previsao.PrevisaoPrecipitacao;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.persistence.EntityManager;

/**
 *
 * @author alan.oliveira
 */
public class Parser {

    public Parser() {}
    
    public List<PrevisaoPrecipitacao> parse(Path path, Properties properties, AquisicaoPrecipitacao aquisicao, EntityManager em) throws IOException, ParseException, ObjectNotFoundException {
        
        Reader reader = new Reader();
        List<String> lines = reader.get(path);
        List<PrevisaoPrecipitacao> previsoes = new ArrayList();
        
        for(String line : lines)
        {
            previsoes.addAll(this.parse(line, properties, aquisicao, em));
        }
        
        return previsoes;
        
    }

    public List<PrevisaoPrecipitacao> parse(String value, Properties properties, AquisicaoPrecipitacao aquisicao, EntityManager em) throws java.text.ParseException, ObjectNotFoundException {

        DateFormat dateFormat = new SimpleDateFormat(properties.getProperty("dateformat", "yyyyMMddHH"));                
        
        String[] array = value.split(";");
        
        if (array.length == 7) {
            
            Long codIBGE;           
            
            if(array[1].toCharArray().length > 7)
            {
                codIBGE = Long.parseLong(array[1].substring(0, 7));
            }
            else
            {
                codIBGE = Long.parseLong(array[1].toString());
            }            
            
            MunicipioRepository municipios = new MunicipioRepository(em);
            Municipio municipio = municipios.getByGeoCodigo(codIBGE);
            if(municipio == null)
            {
                throw new ObjectNotFoundException("Object type " + Municipio.class.getName() + " not found for codIBGE = " + codIBGE.toString());
            }
            
            Double lat = Double.parseDouble(array[2].toString());
            Double lon = Double.parseDouble(array[3].toString());                                    

            List<PrevisaoPrecipitacao> previsoes = new ArrayList();

            for (int i = 6; i > 3; i--) 
            {                
                String[] values = array[i].split(":");                                
                
                PrevisaoPrecipitacao previsao = new PrevisaoPrecipitacao();
                previsao.setDatahora(dateFormat.parse(values[0]));
                previsao.setLatitude(lat);
                previsao.setLongitude(lon);
                try{
                    previsao.setValor(new BigDecimal(values[1]).setScale(2, RoundingMode.HALF_EVEN));                
                }
                catch(NumberFormatException ex) {
                    previsao.setValor(null);
                }
                previsao.setMunicipio(municipio);
                previsao.setAquisicao(aquisicao);                               
                
                previsoes.add(previsao);
            }
            
            return previsoes;
        }
        return null;
    }

}
